package com.neusoft.neuiotms.dcom.converter.change;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.neusoft.bizcore.web.dto.AbstractConverter;
import com.neusoft.neuiotms.dcom.dto.change.ChangeAssignmentDTO;
import com.neusoft.neuiotms.dcom.model.change.ChangeAssignment;
import com.neusoft.neuiotms.dcom.repository.change.ChangeAssignmentRepository;

@Component
public class ChangeAssignmentConverter extends AbstractConverter<ChangeAssignment, ChangeAssignmentDTO> {

    @Autowired
    private ChangeAssignmentRepository changeAssignmentRepository;

    @Override
    public ChangeAssignment toModel(final ChangeAssignmentDTO dto) {
        ChangeAssignment model;
        if (dto.isNew()) {
            model = new ChangeAssignment();
        } else {
            model = this.changeAssignmentRepository.getOne(dto.getId());
        }
        model.setTaskId(dto.getTaskId());
        model.setWorkOrderNum(dto.getWorkOrderNum());
        model.setExecutorCode(dto.getExecutorCode());
        model.setExecutor(dto.getExecutor());
        model.setUpdateTime(new Date());

        return model;
    }

    @Override
    public ChangeAssignmentDTO toDTO(final ChangeAssignment model, final boolean forListView) {
        final ChangeAssignmentDTO dto = new ChangeAssignmentDTO();
        dto.setId(model.getId());
        dto.setTaskId(model.getTaskId());
        dto.setWorkOrderNum(model.getWorkOrderNum());
        dto.setExecutorCode(model.getExecutorCode());
        dto.setExecutor(model.getExecutor());

        dto.setCreateTime(model.getCreateTime());
        dto.setUpdateTime(model.getUpdateTime());

        return dto;
    }

}
